package com.newhero.product.module.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.newhero.product.common.service.impl.BaseServiceImpl;
import com.newhero.product.module.sys.mapper.AppMapper;
import com.newhero.product.module.sys.model.App;
import com.newhero.product.module.sys.service.IAppService;

@Service
public class AppServiceImpl extends BaseServiceImpl<AppMapper, App> implements
		IAppService {

	@Autowired
	private AppMapper appMapper;

	@Override
	public List<Map<String, Object>> getTopMenuListWithAuth(String userID) {
		return appMapper.getTopMenuListWithAuth(userID);
	}

	/*
	 * (non-Javadoc)
	 * @see com.newhero.product.module.sys.service.IAppService#getNextLevelMenuListWithAuth(java.lang.String, java.util.List)
	 */
	@Override
	public List<Map<String, Object>> getNextLevelMenuListWithAuth(String userID, List<Map<String, Object>> topMenuList) {
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		List<String> idList = new ArrayList<String>();
		
		for (Map<String, Object> map : topMenuList) {
			String appPID = map.get("APPID").toString();
			idList.add(appPID);
		}
		if(idList.size()==0){
			return resultList;
		}else{
			resultList = appMapper.getNextLevelMenuListWithAuth(userID, idList);
		}
		
		return resultList;
	}
	/*
	 * (non-Javadoc)
	 * @see com.newhero.product.module.sys.service.IAppService#getNextLevelMenuListWithAuth(java.lang.String, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getNextLevelMenuListWithAuth(String userID, String appID) {
		List<String> idList = new ArrayList<String>();
		idList.add(appID);
		List<Map<String, Object>> resultList = appMapper.getNextLevelMenuListWithAuth(userID, idList);
		return resultList;
	}
	@Override
	public List<String> getIDListByAppID(String appID) {
		/**
		 * 由于各种数据库兼容性问题，就不用SQL查了，用程序查
		 */
		List<String> idList = new ArrayList<String>();
		idList.add(appID);
		genIDListByAppID(appID, idList);

		return idList;
	}
	
	/**
	 * 递归办法
	 * @param appID
	 * @param idList
	 */
	private void genIDListByAppID(String appID, List<String> idList) {
		Map<String, Object> columnMap2 = new HashMap<String, Object>();
		columnMap2.put("appPID", appID);
		List<App> appList2 = selectByMap(columnMap2);
		for (App a : appList2) {
			String id = a.getAppID();
			idList.add(id);
			genIDListByAppID(id, idList);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.newhero.product.module.sys.service.IAppService#selectAppUrlSetByUserID(java.lang.String)
	 */
	@Override
	public Set<String> selectAppUrlSetByUserID(String userID) {
		List<String> urlList = appMapper.selectAppUrlListByUserID(userID);
		Set<String> resultSet = new HashSet<String>();
		for(String url : urlList){
			if(StringUtils.isNotEmpty(url)){
				resultSet.add(url);
			}
		}
		return resultSet;
	}

	@Override
	public List<Map<String, Object>> getNextLevelMenuListWithAuth(
			String userID, int appType, List<Map<String, Object>> topMenuList) {
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		List<String> idList = new ArrayList<String>();
		
		for (Map<String, Object> map : topMenuList) {
			String appPID = map.get("APPID").toString();
			idList.add(appPID);
		}
		if(idList.size()==0){
			return resultList;
		}else{
			resultList = appMapper.getNextLevelMenuListWithAuthForBis(userID, appType, idList);
		}
		
		return resultList;
	}



}
